home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / mipsABI / examples / sup / supcmisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.2 KB  |  355 lines

  1. /*
  2.  * Copyright (c) 1992 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  12.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  13.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  14.  *
  15.  * Carnegie Mellon requests users of this software to return to
  16.  *
  17.  *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
  18.  *  School of Computer Science
  19.  *  Carnegie Mellon University
  20.  *  Pittsburgh PA 15213-3890
  21.  *
  22.  * any improvements or extensions that they make and grant Carnegie Mellon
  23.  * the rights to redistribute these changes.
  24.  */
  25. /*
  26.  * sup misc. routines, include list processing.
  27.  **********************************************************************
  28.  * HISTORY
  29.  * $Log: supcmisc.c,v $
  30.  * Revision 1.1.1.1  1993/05/21  14:52:18  cgd
  31.  * initial import of CMU's SUP to NetBSD
  32.  *
  33.  * Revision 1.5  92/08/11  12:07:22  mrt
  34.  *     Added release to FILEWHEN name.
  35.  *     Brad's changes: delinted and updated variable arguement usage.
  36.  *     [92/07/26            mrt]
  37.  * 
  38.  * Revision 1.3  89/08/15  15:31:28  bww
  39.  *     Updated to use v*printf() in place of _doprnt().
  40.  *     From "[89/04/19            mja]" at CMU.
  41.  *     [89/08/15            bww]
  42.  * 
  43.  * 27-Dec-87  Glenn Marcy (gm0w) at Carnegie-Mellon University
  44.  *    Fixed bug in ugconvert() which left pw uninitialized.
  45.  *
  46.  * 25-May-87  Doug Philips (dwp) at Carnegie-Mellon University
  47.  *    Split off from sup.c and changed goaway to use printf
  48.  *    instead of notify if thisC is NULL.
  49.  *
  50.  **********************************************************************
  51.  */
  52.  
  53. #if __STDC__
  54. #include <stdarg.h>
  55. #else
  56. #include <varargs.h>
  57. #endif
  58. #include "supcdefs.h"
  59.  
  60. #if __STDC__
  61. int notify (char *, ...);
  62. #endif
  63.  
  64. struct liststruct {        /* uid and gid lists */
  65.     char *Lname;        /* name */
  66.     int Lnumber;        /* uid or gid */
  67.     struct liststruct *Lnext;
  68. };
  69. typedef struct liststruct LIST;
  70.  
  71. #define HASHBITS    4
  72. #define HASHSIZE    (1<<HASHBITS)
  73. #define HASHMASK    (HASHSIZE-1)
  74. #define LISTSIZE    (HASHSIZE*HASHSIZE)
  75.  
  76. static LIST *uidL[LISTSIZE];        /* uid and gid lists */
  77. static LIST *gidL[LISTSIZE];
  78.  
  79. extern COLLECTION *thisC;        /* collection list pointer */
  80.  
  81. /*************************************************
  82.  ***    P R I N T   U P D A T E   T I M E S    ***
  83.  *************************************************/
  84.  
  85. prtime ()
  86. {
  87.     char buf[STRINGLENGTH];
  88.     char relsufix[STRINGLENGTH];
  89.     long twhen;
  90.     int f;
  91.  
  92.     if ((thisC->Cflags&CFURELSUF) && thisC->Crelease)
  93.         (void) sprintf (relsufix,".%s",thisC->Crelease);
  94.     else
  95.         relsufix[0] = '\0';
  96.     if (chdir (thisC->Cbase) < 0)
  97.         logerr ("Can't change to base directory %s for collection %s",
  98.             thisC->Cbase,thisC->Cname);
  99.     (void) sprintf (buf,FILEWHEN,thisC->Cname,relsufix);
  100.     f = open (buf,O_RDONLY,0);
  101.     if (f >= 0) {
  102.         if (read(f,(char *)&twhen,sizeof(long)) != sizeof(long))
  103.             twhen = 0;
  104.         (void) close (f);
  105.     } else
  106.         twhen = 0;
  107.     (void) strcpy (buf,ctime (&twhen));
  108.     buf[strlen(buf)-1] = '\0';
  109.     loginfo ("Last update occurred at %s for collection %s",
  110.         buf,thisC->Cname);
  111. }
  112.  
  113. int establishdir (fname)
  114. char *fname;
  115. {
  116.     char dpart[STRINGLENGTH],fpart[STRINGLENGTH];
  117.     path (fname,dpart,fpart);
  118.     return (estabd (fname,dpart));
  119. }
  120.  
  121. int estabd (fname,dname)
  122. char *fname,*dname;
  123. {
  124.     char dpart[STRINGLENGTH],fpart[STRINGLENGTH];
  125.     struct stat sbuf;
  126.     register int x;
  127.  
  128.     if (stat (dname,&sbuf) >= 0)  return (FALSE); /* exists */
  129.     path (dname,dpart,fpart);
  130.     if (strcmp (fpart,".") == 0) {        /* dname is / or . */
  131.         notify ("SUP: Can't create directory %s for %s\n",dname,fname);
  132.         return (TRUE);
  133.     }
  134.     x = estabd (fname,dpart);
  135.     if (x)  return (TRUE);
  136.     (void) mkdir (dname,0755);
  137.     if (stat (dname,&sbuf) < 0) {        /* didn't work */
  138.         notify ("SUP: Can't create directory %s for %s\n",dname,fname);
  139.         return (TRUE);
  140.     }
  141.     vnotify ("SUP Created directory %s for %s\n",dname,fname);
  142.     return (FALSE);
  143. }
  144.  
  145. /***************************************
  146.  ***    L I S T   R O U T I N E S    ***
  147.  ***************************************/
  148.  
  149. static
  150. int Lhash (name)
  151. char *name;
  152. {
  153.     /* Hash function is:  HASHSIZE * (strlen mod HASHSIZE)
  154.      *              +          (char   mod HASHSIZE)
  155.      * where "char" is last character of name (if name is non-null).
  156.      */
  157.  
  158.     register int len;
  159.     register char c;
  160.  
  161.     len = strlen (name);
  162.     if (len > 0)    c = name[len-1];
  163.     else        c = 0;
  164.     return (((len&HASHMASK)<<HASHBITS)|(((int)c)&HASHMASK));
  165. }
  166.  
  167. static
  168. Linsert (table,name,number)
  169. LIST **table;
  170. char *name;
  171. int number;
  172. {
  173.     register LIST *l;
  174.     register int lno;
  175.     lno = Lhash (name);
  176.     l = (LIST *) malloc (sizeof(LIST));
  177.     l->Lname = name;
  178.     l->Lnumber = number;
  179.     l->Lnext = table[lno];
  180.     table[lno] = l;
  181. }
  182.  
  183. static
  184. LIST *Llookup (table,name)
  185. LIST **table;
  186. char *name;
  187. {
  188.     register int lno;
  189.     register LIST *l;
  190.     lno = Lhash (name);
  191.     for (l = table[lno]; l && strcmp(l->Lname,name) != 0; l = l->Lnext);
  192.     return (l);
  193. }
  194.  
  195. ugconvert (uname,gname,uid,gid,mode)
  196. char *uname,*gname;
  197. int *uid,*gid,*mode;
  198. {
  199.     register LIST *u,*g;
  200.     register struct passwd *pw;
  201.     register struct group *gr;
  202.     struct stat sbuf;
  203.     static int defuid = -1;
  204.     static int defgid;
  205.     static int first = TRUE;
  206.  
  207.     if (first) {
  208.         bzero ((char *)uidL, sizeof (uidL));
  209.         bzero ((char *)gidL, sizeof (gidL));
  210.         first = FALSE;
  211.     }
  212.     pw = NULL;
  213.     if (u = Llookup (uidL,uname))
  214.         *uid = u->Lnumber;
  215.     else if (pw = getpwnam (uname)) {
  216.         Linsert (uidL,salloc(uname),pw->pw_uid);
  217.         *uid = pw->pw_uid;
  218.     }
  219.     if (u || pw) {
  220.         if (g = Llookup (gidL,gname)) {
  221.             *gid = g->Lnumber;
  222.             return;
  223.         }
  224.         if (gr = getgrnam (gname)) {
  225.             Linsert (gidL,salloc(gname),gr->gr_gid);
  226.             *gid = gr->gr_gid;
  227.             return;
  228.         }
  229.         if (pw == NULL)
  230.             pw = getpwnam (uname);
  231.         *mode &= ~S_ISGID;
  232.         *gid = pw->pw_gid;
  233.         return;
  234.     }
  235.     *mode &= ~(S_ISUID|S_ISGID);
  236.     if (defuid >= 0) {
  237.         *uid = defuid;
  238.         *gid = defgid;
  239.         return;
  240.     }
  241.     if (stat (".",&sbuf) < 0) {
  242.         *uid = defuid = getuid ();
  243.         *gid = defgid = getgid ();
  244.         return;
  245.     }
  246.     *uid = defuid = sbuf.st_uid;
  247.     *gid = defgid = sbuf.st_gid;
  248. }
  249.  
  250.  
  251. /*********************************************
  252.  ***    U T I L I T Y   R O U T I N E S    ***
  253.  *********************************************/
  254.  
  255. #if __STDC__
  256. notify (char *fmt,...)        /* record error message */
  257. #else
  258. /*VARARGS*//*ARGSUSED*/
  259. notify (va_alist)        /* record error message */
  260. va_dcl
  261. #endif
  262. {
  263. #if !__STDC__
  264.     char *fmt;
  265. #endif
  266.     char buf[STRINGLENGTH];
  267.     char collrelname[STRINGLENGTH];
  268.     long tloc;
  269.     static FILE *noteF = NULL;    /* mail program on pipe */
  270.     va_list ap;
  271.  
  272. #if __STDC__
  273.     va_start(ap,fmt);
  274. #else
  275.     va_start(ap);
  276.     fmt = va_arg(ap,char *);
  277. #endif
  278.     if (fmt == NULL) {
  279.         if (noteF && noteF != stdout)
  280.             (void) pclose (noteF);
  281.         noteF = NULL;
  282.         return;
  283.     }
  284.     if ((thisC->Cflags&CFURELSUF) && thisC->Crelease) 
  285.         (void) sprintf (collrelname,"%s-%s",collname,thisC->Crelease);
  286.     else
  287.         (void) strcpy (collrelname,collname);
  288.     
  289.     if (noteF == NULL) {
  290.         if ((thisC->Cflags&CFMAIL) && thisC->Cnotify) {
  291.             (void) sprintf (buf,"mail -s \"SUP Upgrade of %s\" %s >/dev/null",
  292.                 collrelname,thisC->Cnotify);
  293.             noteF = popen (buf,"w");
  294.             if (noteF == NULL) {
  295.                 logerr ("Can't send mail to %s for %s",
  296.                     thisC->Cnotify,collrelname);
  297.                 noteF = stdout;
  298.             }
  299.         } else
  300.             noteF = stdout;
  301.         tloc = time ((long *)NULL);
  302.         fprintf (noteF,"SUP Upgrade of %s at %s",
  303.             collrelname,ctime (&tloc));
  304.         (void) fflush (noteF);
  305.     }
  306.     vfprintf(noteF,fmt,ap);
  307.     va_end(ap);
  308.     (void) fflush (noteF);
  309. }
  310.  
  311. lockout (on)        /* lock out interrupts */
  312. int on;
  313. {
  314. #ifdef _ABI_SOURCE
  315.     sigset_t x, lockmask;
  316.  
  317.     if (on) {
  318.         sigemptyset (&x);
  319.         sigaddset (&x, SIGHUP);
  320.         sigaddset (&x, SIGINT);
  321.         sigaddset (&x, SIGQUIT);
  322.         sigaddset (&x, SIGTERM);
  323.         (void) sigprocmask (SIG_BLOCK, &x, &lockmask);
  324.     }
  325.     else {
  326.         (void) sigprocmask (SIG_SETMASK, &lockmask, NULL);
  327.     }
  328. #else
  329.     register int x;
  330.     static int lockmask;
  331.  
  332.     if (on) {
  333.         x = sigmask (SIGHUP) | sigmask (SIGINT) |
  334.             sigmask (SIGQUIT) | sigmask (SIGTERM);
  335.         lockmask = sigblock (x);
  336.     }
  337.     else {
  338.         (void) sigsetmask (lockmask);
  339.     }
  340. #endif
  341. }
  342.  
  343. char *fmttime (time)
  344. long time;
  345. {
  346.     static char buf[STRINGLENGTH];
  347.     int len;
  348.  
  349.     (void) strcpy (buf,ctime (&time));
  350.     len = strlen(buf+4)-6;
  351.     (void) strncpy (buf,buf+4,len);
  352.     buf[len] = '\0';
  353.     return (buf);
  354. }
  355.